Simple Explanation:

This project detects hand gestures using MediaPipe in Python. The number of raised fingers is counted and sent to Arduino, which then turns on the corresponding number of LEDs. 🖐️➡️💡

 

🛠️ Requirements

🔹 Required Components

Component

Description

Arduino Uno

Microcontroller to process data.

Webcam

Captures real-time video for hand detection.

5 LEDs

LEDs that light up based on the detected fingers.

Resistors

Protect LEDs from excessive current.

Jumper wires

Used for wiring connections.

Computer

Runs the Python script.

🔹 Required Libraries

Library

Installation Command

Purpose

OpenCV

pip install opencv-python

Used for image processing.

MediaPipe

pip install mediapipe

Detects hand gestures.

PySerial

pip install pyserial

Enables communication between Python & Arduino.

🔹 Required Software

SoftwareInstallation LinkPurpose
Python (pip)Download PythonRuns the hand tracking script.
Arduino IDEDownload Arduino IDEUploads code to the Arduino board.
VS CodeDownload VS CodeCode editor for Python and Arduino.

📌 Steps to Follow:

🖥️ 1- Setting Up Python Environment:

make sure that you install the Python program and activate the pip function.

Then Open CMD in your PC and copy this comand

				
					pip install opencv-python mediapipe pyserial
				
			

🔌 2- Connecting the Components:

💡 Connect 5 LEDs to Arduino pins 6-10 with resistors to limit current.

🔧 3- Uploading the Arduino Code:

Make sure you select the right port then upload this code to your Arduino using Arduino IDE:

				
					int ledPins[] = {6,7,8,9,10}; // Pins for 5 LEDs
int numLeds = 5;

void setup() {
  // Initialize LED pins as outputs
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    // Read the number of fingers from serial input
    int fingers = Serial.parseInt();

    // Turn on LEDs based on the number of fingers
    for (int i = 0; i < numLeds; i++) {
      if (i < fingers) {
        digitalWrite(ledPins[i], HIGH); // Turn on LED
      } else {
        digitalWrite(ledPins[i], LOW); // Turn off LED
      }
    }
  }
}

				
			

2️⃣ Run the Python Code Below:

 please check the Python program you selected same port as the Arduino.
				
					import cv2
import mediapipe as mp
import serial
import time

# Initialize serial communication (update COM port as per your system)
arduino = serial.Serial('COM4', 9600)  # Replace 'COM4' with your Arduino port
time.sleep(2)  # Wait for Arduino to initialize

# Initialize MediaPipe Hand Detection
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mp_draw = mp.solutions.drawing_utils

# Helper function to count raised fingers
def count_raised_fingers(hand_landmarks, handedness_label):
    finger_tips = [8, 12, 16, 20]  # Index for fingertips
    thumb_tip = 4  # Thumb tip
    thumb_mcp = 2  # Thumb MCP (base)
    count = 0

    landmarks = hand_landmarks.landmark

    # Check fingers (tip above middle joint)
    for tip in finger_tips:
        if landmarks[tip].y < landmarks[tip - 2].y:
            count += 1

    # REVERSED: Treat Right as Left, and Left as Right
    if handedness_label == 'Left':
        if landmarks[thumb_tip].x < landmarks[thumb_mcp].x:
            count += 1
    else:  # Right hand (treated as Left)
        if landmarks[thumb_tip].x > landmarks[thumb_mcp].x:
            count += 1

    return count

# OpenCV video capture
cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    if not success:
        break

    # Convert to RGB for MediaPipe
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(img_rgb)

    if results.multi_hand_landmarks and results.multi_handedness:
        for hand_landmarks, handedness in zip(results.multi_hand_landmarks, results.multi_handedness):
            # Get hand label (Left or Right) and REVERSE it
            actual_label = handedness.classification[0].label
            swapped_label = 'Left' if actual_label == 'Right' else 'Right'

            # Draw landmarks
            mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)

            # Count raised fingers
            finger_count = count_raised_fingers(hand_landmarks, swapped_label)

            # Display swapped hand and finger count
            cv2.putText(img, f"{swapped_label} Hand", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
            cv2.putText(img, f"Fingers: {finger_count}", (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

            # Send finger count to Arduino
            arduino.write(f"{finger_count}\n".encode())

    # Show the image
    cv2.imshow("Hand Tracking", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Cleanup
cap.release()
cv2.destroyAllWindows()
arduino.close()

				
			
🚀 Testing the Project:

1️⃣ Upload the Arduino code first.

2️⃣ Run the Python code and wait for the camera to start.

3️⃣ Raise your fingers in front of the camera:

  • 1 finger = 1 LED ON 💡
  • 2 fingers = 2 LEDs ON 💡💡
  • All fingers = All LEDs ON! 🔥

4️⃣ Close your hand (0 fingers) = All LEDs OFF 🔄

💬 I would love to hear your thoughts and feedback! Drop a comment or rate this project! 😊
Previous